package com.miao;


import com.miao.entity.Student;
import com.miao.service.IPeopleService;
import com.miao.service.IUserService;
import com.miao.service.Impl.PeopleServiceImpl;
import com.miao.service.Impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;


/**
* Description:  XML方式 定义Bean
* Author: Miao@ShaoQing
* Data: 9:24 2021/4/1
*/
public class MyTest01 {

    /**
    * Description: XML方式定义普通的Bean
    * Author: Miao@ShaoQing
    * Data: 10:48 2021/4/1
    */
    @Test
    public void test1(){

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("BeanConfig1.xml");

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //从容器里面取 Bean对象 Student1。
        Student student = (Student) applicationContext.getBean("student1");
        System.out.println(student);
    }


    /**
    * Description:  XML方式定义Bean，存在Bean依赖，显示注入依赖
    * Author: Miao@ShaoQing
    * Data: 10:48 2021/4/1
    */
    @Test
    public void test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("BeanConfig2.xml");

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //调用Service的方法
        //从容器里面取 Bean对象 IUserService
        IUserService userService = (UserServiceImpl) applicationContext.getBean("UserServiceImpl");
        userService.printUser();
    }


    /**
     * Description:  XML方式定义Bean，存在Bean依赖，自动装配AutoWired
     * Author: Miao@ShaoQing
     * Data: 10:48 2021/4/1
     */
    @Test
    public void test3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("BeanConfig3.xml");

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //调用Service的方法
        //从容器里面取 Bean对象 IUserService
        IUserService userService = (UserServiceImpl) applicationContext.getBean("UserServiceImpl");
        userService.printUser();
    }


    /**
     * Description:  XML方式定义Bean，存在Bean依赖， 使用注解 @AutoWired  @Resource
     * Author: Miao@ShaoQing
     * Data: 10:48 2021/4/1
     */
    @Test
    public void test4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("BeanConfig4.xml");

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //调用Service的方法
        //从容器里面取 Bean对象 IUserService
        IPeopleService peopleService = (PeopleServiceImpl) applicationContext.getBean("PeopleServiceImpl");
        peopleService.printPeople();
    }








}
